package com.iqiyi.pps.epg.core.service.statistic;

import com.iqiyi.pps.epg.api.model.web.query.TFilter;
import com.iqiyi.pps.epg.api.model.web.query.TPage;
import com.iqiyi.pps.epg.api.model.web.statistic.TStatisticOcrNewData;
import com.iqiyi.pps.epg.api.model.web.statistic.TStatisticOcrNewDataPage;
import com.iqiyi.pps.epg.core.dao.base.Page;
import com.iqiyi.pps.epg.core.dao.base.PropertyFilter;
import com.iqiyi.pps.epg.core.dao.statistic.StatisticOcrNewDataDao;
import com.iqiyi.pps.epg.core.model.statistic.StatisticOcrNewData;
import com.iqiyi.pps.epg.core.utils.DateTimeUtil;
import com.iqiyi.pps.epg.core.utils.TransUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhang on 2016/1/25.
 */
@Service
@Transactional
public class StatisticOcrNewDataServiceImpl implements StatisticOcrNewDataService {
    protected Logger logger = LoggerFactory.getLogger(StatisticOcrNewDataServiceImpl.class);
    @Autowired
    private StatisticOcrNewDataDao dao;

    @Override
    public long save(StatisticOcrNewData entity) {
        dao.saveOnly(entity);
        return entity.getId();
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public TStatisticOcrNewDataPage findTStatisticOcrNewDataPage(TPage page, TFilter filter) {
        List<PropertyFilter> filterList = TransUtil.builderFilterList(filter);
        Page<StatisticOcrNewData> dbPage = TransUtil.buildPage(page);
        Page<StatisticOcrNewData> pageData = dao.findPage(dbPage, filterList);
        List<StatisticOcrNewData> dataList = pageData.getResult();
        TStatisticOcrNewDataPage carouselPage = new TStatisticOcrNewDataPage();
        page.setTotalCount(pageData.getTotalCount());
        carouselPage.setTPage(page);
        List<TStatisticOcrNewData> resultList = transToThriftList(dataList);
        carouselPage.setResult(resultList);
        return carouselPage;
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public String getOcrNewAvgRate(TFilter filter) {
        String hql = "SELECT avg(percent) from StatisticOcrNewData ";
        List<PropertyFilter> filterList = TransUtil.builderFilterList(filter);
        hql = TransUtil.buildQueryStringByPropertyFilter(hql, filterList);
        Object result = dao.createQuery(hql).uniqueResult();
        return result == null ? "0" : result + "";
    }

    //count(id) ,sum( IF( percent = 100 , 1,0 ) ) sumNum
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public String getOcrFullPercentRate(TFilter filter) {
        // sum(case bd.domainType when 1 then bd.domainQry else 0 end) 替代 if
        String hql = "SELECT count(id) ,sum( case percent when 100 then 1 else 0 end  ) from StatisticOcrNewData ";
        List<PropertyFilter> filterList = TransUtil.builderFilterList(filter);
        hql = TransUtil.buildQueryStringByPropertyFilter(hql, filterList);
        List result = dao.createQuery(hql).list();
        Object[] obj = (Object[]) result.get(0);
        logger.info("count {} , sum {}", obj[0], obj[1]);
        if (Integer.valueOf(obj[0] + "") == 0) {
            return "0";
        }
        double rate = Integer.valueOf(obj[1] + "") * 100.0 / Integer.valueOf(obj[0] + "");
        return rate +"";
    }

    private List<TStatisticOcrNewData> transToThriftList(List<StatisticOcrNewData> dataList) {
        List<TStatisticOcrNewData> resultList = new ArrayList<>();
        TStatisticOcrNewData tChannelLog = null;
        for (StatisticOcrNewData tempData : dataList) {
            tChannelLog = transToThrift(tempData);
            resultList.add(tChannelLog);
        }
        return resultList;
    }

    private TStatisticOcrNewData transToThrift(StatisticOcrNewData tempData) {
        TStatisticOcrNewData thriftData = new TStatisticOcrNewData();
        thriftData.setId(tempData.getId());
        thriftData.setUser(tempData.getUser());
        thriftData.setAddTime(DateTimeUtil.transLongFormat(tempData.getAddTime().getTime()));
        thriftData.setOcrTitle(tempData.getOcrTitle());
        thriftData.setUserTitle(tempData.getUserTitle());
        thriftData.setOcrFlag(tempData.getOcrFlag());
        thriftData.setImageUrl(tempData.getImageUrl());
        thriftData.setLocations(tempData.getLocations());
        thriftData.setOcrTitleCharLen(tempData.getOcrTitleCharLen());
        thriftData.setUserTitleCharLen(tempData.getUsrTitleCharLen());
        thriftData.setLiveBeginTime(DateTimeUtil.transLongFormat(tempData.getLiveBeginTime().getTime()));
        thriftData.setLiveEndTime(DateTimeUtil.transLongFormat(tempData.getLiveEndTime().getTime()));
        thriftData.setPercent(tempData.getPercent());
        thriftData.setChannelName(tempData.getChannelName());
        thriftData.setLegoProgramId(tempData.getLegoProgramId());
        return thriftData;
    }
}
